package app.modules.cloudpivot.permission;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;

import com.authine.cloudpivot.IEngine;
import com.authine.cloudpivot.engine.bizobject.filter.FilterExpression;
import com.authine.cloudpivot.engine.bizobject.filter.FilterExpression.Op;
import com.authine.cloudpivot.engine.bizobject.filter.FilterExpression.Or;
import com.authine.cloudpivot.engine.permission.DataPermissionRuntimeServiceImpl;
import com.authine.cloudpivot.engine.permission.Q;

public class CustomDataPermissionRuntimeServiceImpl extends DataPermissionRuntimeServiceImpl {
    private static final Logger LOG = LoggerFactory.getLogger(CustomDataPermissionRuntimeServiceImpl.class);

    public CustomDataPermissionRuntimeServiceImpl(IEngine engine, Environment env) {
        super(engine, env);
    }

    @Override
    public FilterExpression getQueryFilterExpression(String userId, String schemaCode, boolean isOwner) {
        FilterExpression expression = super.getQueryFilterExpression(userId, schemaCode, isOwner);
        LOG.info("===>>执行自定义数据权限:{}", expression.getClass().getCanonicalName());
        if (expression.isEmpty()) {
            /* 如果自带是空那么就是查询所有 二开不需要处理 */
            return expression;
        } else if (expression instanceof FilterExpression.Item) {
            /* 如果自带是FilterExpression.Item那么就是均不可见 二开只需要返回自己的逻辑就行 */
            return Q.it("id", Op.Eq, "");
        } else if (expression instanceof FilterExpression.Or) {
            /* 如果自带是FilterExpression.Or那么就是存在权限组的 二开追加权限即可 */
            FilterExpression.Or permission = (Or) expression;
            return permission;
        } else {
            return expression;
        }
    }
}
